package com.ingenio.siot.adapter;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ingenio.siot.Adapter;
import com.ingenio.siot.entity.Permiso;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class PermisoAdapter extends Adapter{
	
	public List<Permiso> devolverTodos() throws Exception{
    	Connection conn = null;
    	List<Permiso> permisos = new ArrayList<Permiso>();
    	PreparedStatement stmt = null;
		try {
			conn = (Connection) abrirConexion();
			String sel ="SELECT * FROM permisos WHERE activo=1";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		ResultSet result = stmt.executeQuery();
    		Permiso p = null;
    		
    		while(result.next()){
    			Integer id = result.getInt("IdPermiso");
    			String desc = result.getString("descPermiso");
    			boolean activo = result.getBoolean("activo");
    			p = new Permiso(id, desc, activo); 
    			permisos.add(p);
    		}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		//conn.close();
    		cerrarConexion(conn);
    	}
    	return permisos;
    }
	
	public Permiso devolverPermiso(Integer idPermiso) throws Exception{
		Connection conn = null;
		Permiso p = null;
		PreparedStatement stmt = null;
		try{
			conn = (Connection) abrirConexion();
			String query = "SELECT * FROM permisos WHERE IdPermiso=? and activo=1";
			stmt = (PreparedStatement) conn.prepareStatement(query);
			stmt.setInt(1, idPermiso);
			ResultSet result = stmt.executeQuery();
			if(result.next()){
				Integer id = result.getInt("IdPermiso");
				String desc = result.getString("descPermiso");
				boolean activo = result.getBoolean("activo");
				p = new Permiso(id, desc, activo); 
			}
		}
		catch (Exception e){
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos", e);
		}
		finally{
			cerrarConexion(conn);
		}
		return p;
	}
	
	public Permiso crearPermiso(Permiso per) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		PermisoAdapter pa = new PermisoAdapter();
		Permiso p = null;
    	try {
    		conn = (Connection) abrirConexion();
    		String query ="INSERT INTO permisos(descPermiso,activo) VALUES(?,?)";
    		stmt = (PreparedStatement) conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
    		stmt.setString(1,per.getDescPermiso());
    		stmt.setBoolean(2,per.isActivo());
    		stmt.execute();
    		ResultSet id = stmt.getGeneratedKeys();
    		if (id.next()){
    			p = pa.devolverPermiso(id.getInt(1));
    		}
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
    	return p;
	}
	
	public void editarPermiso(Permiso per) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
    		conn = (Connection) abrirConexion();
    		String query =	"UPDATE permisos " +
    						"SET descPermiso=?, activo=? " +
    						"WHERE IdPermiso=?";
    		stmt = (PreparedStatement) conn.prepareStatement(query);
    		stmt.setString(1,per.getDescPermiso());
    		stmt.setBoolean(2,per.isActivo());
    		stmt.setInt(3,per.getIdPermiso());
    		stmt.execute();
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
	}
	
	public void eliminarPermiso(Integer idPermiso) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
    	try {
    		conn = (Connection) abrirConexion();
    		String query =  "UPDATE permisos SET activo=0 WHERE IdPermiso=?";
    		stmt = (PreparedStatement) conn.prepareStatement(query);
    		stmt.setInt(1,idPermiso);
    		stmt.execute();
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
	}
}
